package com.wee.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wee.aircoach_user.PhysicalTestActivity;
import com.wee.aircoach_user.R;

public class tab_FragmentTest2 extends Fragment implements OnClickListener{
	/**cc传递数据的 是 int 类型的跳转第几个fragment    int类型state代表是否可以跳转 **/

	private View mMainView;
	public tab_FragmentTest2() {
		super();
	}

	View rootView;

	//开始
	private TextView start_txt,num_num;
	//完成下一个按钮
	private ImageButton btn;
	//重新测试
	private LinearLayout qw;
	private  int reclen;
	 private int value0,value1 = -1;

	private static int STATE_START = 0;
	private static int STATE_STOP = 1;
	private int state = STATE_STOP;
	//1代表测试完成可点击
	public static final int MSG_TIME_SRART = 1;
	public static final int MSG_TIME_RUNNGIN = 2;
	public static final int MSG_TIME_END = 3;
	public static final int MSG_TIME_READ = 4;
	private int timeValue = 0;
	private TimeTask task;
	private Handler mHandler = new Handler(){
		/**测试线程记录数据 timeValue 为记录次数 一秒加一
		 * 因为不是俯卧撑运动所以直接得到start_txt 数据*/
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			String text = "";
			if(msg.what == MSG_TIME_SRART){
				timeValue = 0;
				text = timeValue +"";
			}else if(msg.what == MSG_TIME_END){
			//	text = "";
				if(timeValue>0){
					updateTestValue(timeValue);
					num_num.setText("开始");
				}else {
					Toast.makeText(getActivity(), "测试时间太短，请重新开始", Toast.LENGTH_SHORT).show();
				}
			}else if(msg.what == MSG_TIME_RUNNGIN){
				num_num.setVisibility(View.VISIBLE);
				if(state == STATE_START){
					qw.setBackgroundResource(R.drawable.s_top);
				}
				start_txt.setClickable(true);
				     timeValue++;
					  text = timeValue +"";
				    num_num.setText(""+text);
			}else if(msg.what ==MSG_TIME_READ){
				start_txt.setClickable(false);
				reclen--;
				num_num.setText(""+reclen);
				if(reclen==0){
					num_num.setVisibility(View.GONE);
					qw.setBackgroundResource(R.drawable.s_top);
				}
			}

		}
	};

	public interface CallBackToMain1{
		public void callBackMain1(boolean isTrue);
	}
	
	private CallBackToMain1 callBack1;

	public CallBackToMain1 getCallBack1() {
		return callBack1;
	}

	public void setCallBack1(CallBackToMain1 callBack1) {
		this.callBack1 = callBack1;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.zice_fragment2, container, false);
        init();
		return rootView;
	}

	private void init() {
		ImageView imageView = (ImageView)rootView.findViewById(R.id.bg_2);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.pingxiceshi, imageView);
		ImageView imageView1 = (ImageView)rootView.findViewById(R.id.bottom);
		ImageLoader.getInstance().displayImage("drawable://" + R.drawable.white_bottom, imageView1);
		//切换背景
		qw= (LinearLayout) rootView.findViewById(R.id.qw);
		//k开始
		start_txt=(TextView)rootView.findViewById(R.id.btn_start);
		start_txt.setOnClickListener(this);
		//完成跳转
		btn=(ImageButton)rootView.findViewById(R.id.btn1);
		btn.setOnClickListener(this);
		//重新测试

		num_num= (TextView) rootView.findViewById(R.id.num_num);
	}
	private int updateTestValue(int value){
		value1 =value-1;
	 return  value1;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	switch (v.getId()) {
	case R.id.btn1:
		try{
			//判断
				if(callBack1 != null||value1!=-1){
					Bundle args=new Bundle();
					args.putInt("value1", updateTestValue(timeValue));

					((PhysicalTestActivity)getActivity()).setFragmentArguments(args);
					callBack1.callBackMain1(true);
				}else{
					Toast.makeText(getActivity(), "请完成测试项目", Toast.LENGTH_SHORT).show();
				}

		}catch(Exception e){

		}
		break;
		case R.id.btn_start:
			if(state == STATE_STOP){
				btn.setVisibility(View.GONE);
				reclen=4;
				qw.setBackgroundResource(R.drawable.beg_in);
				state = STATE_START;
				task = new TimeTask();
				task.start();
			}else if(state == STATE_START){
				btn.setVisibility(View.VISIBLE);
				qw.setBackgroundResource(R.drawable.again);
				state = STATE_STOP;
				task.stopTask();
			}
			break;

		/**暂时没做重新测试的*/

		default:

			break;
	
	}
		
	}



	public class TimeTask implements Runnable{
		private Thread task;
		private boolean isStop = false;
		public TimeTask(){
			task = new Thread(this);
		}

		@Override
		public void run() {
			mHandler.sendEmptyMessage(MSG_TIME_SRART);
			while (!isStop){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				/**需要倒计时三秒**/
				if (reclen>0){
					mHandler.sendEmptyMessage(MSG_TIME_READ);
				}else{
					mHandler.sendEmptyMessage(MSG_TIME_RUNNGIN);
				}
			}
			mHandler.sendEmptyMessage(MSG_TIME_END);
		}

		public void start(){
			task.start();
		}

		public void stopTask(){
			isStop = true;
		}
	}



}
